#ifndef BASE_SYSTEM_LOG_IO_INCLUDE_IO_DIRECT_IO_STRATEGY_H
#define BASE_SYSTEM_LOG_IO_INCLUDE_IO_DIRECT_IO_STRATEGY_H

#include "file_output_strategy.h"
#include "system_log_config.h"

namespace Base {
namespace SystemLog {
struct DirectIOOptions : FileOutputOptions {
    size_t alignment = DEFAULT_DIRECT_IO_ALIGNMENT;
    size_t bufferSize = DEFAULT_DIRECT_IO_BUFFER_SIZE;
};

class DirectIOStrategy : public FileOutputStrategy {
public:
    explicit DirectIOStrategy(const DirectIOOptions &options);
    ~DirectIOStrategy() override;

    ErrCode Write(const char* data, size_t len) override;
    ErrCode Flush() override;
    ErrCode Close() override;

    std::string GetName() const override {
        return "DirectIOOutput";
    }

private:
    ErrCode OpenFile() override;
    ErrCode WriteAligned(const char* data, size_t size);
    ErrCode FlushBuffer();

    DirectIOOptions dioOptions_;

    // 使用自定义删除器的智能指针管理对齐内存
#ifdef _WIN32
    std::unique_ptr<char, void(*)(void*)> buffer_;
#else
    std::unique_ptr<char, void(*)(void*)> buffer_;
#endif

    size_t bufferOffset_ = 0;

    // 平台特定的文件打开标志
#ifdef _WIN32
    DWORD GetWindowsFlags() const;
#else
    int GetUnixFlags() const;
#endif
};
} // namespace SystemLog
} // namespace Base

#endif // BASE_SYSTEM_LOG_IO_INCLUDE_IO_DIRECT_IO_STRATEGY_H